package com.zhangshitong.springbootModel.demo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhangshitong.springbootModel.base.Page;
import com.zhangshitong.springbootModel.demo.entity.User;
import com.zhangshitong.springbootModel.mapper.UserMapper;


@Component
public class UserService {

	@Autowired
	UserMapper userMapper;
	
	public Object saveUser(User user){
		return userMapper.saveUser(user);
	}
	
	public Object editUser(User user){
		return userMapper.editUser(user);
	}
	
	public Object delUser(String id){
		return userMapper.delUser(id);
	}
	
	public List<User> queryAll(User user){
		return userMapper.queryUsers(user);
	}
	
	/**
	 * 支持模糊查询
	 * 模糊查询用到了两种方法:
	 * 1.在传入xml文件之前，用java代码加上模糊查询"%"
	 * 2.在xml文件中的sql语句使用CONCAT函数
	 * @param page
	 * @param user
	 * @return
	 */
	public PageInfo queryUsers(Page page, User user){
		User u = new User();
		u.setId(user.getId());
		if(user.getName()!=null){
			u.setName("%"+user.getName()+"%");
		}
		u.setAddr(user.getAddr());
		u.setAge(user.getAge());
		return PageHelper.startPage(page.getCurrentPage(), page.getPageSize()).doSelectPage(new ISelect() {
			
			@Override
			public void doSelect() {
				userMapper.queryUsers(u);
				
			}
		}).toPageInfo();
	}
}
